package com.sls.game.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.sls.game.dao.GoodsInfoManageDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.GoodsManageInfo;
import com.sls.game.pojo.SpecialPriceSystemInfo;

/**
 * 
 * @description 商品信息管理
 * @author Wangxuegang
 * @date 2012-9-14
 * 
 */
@Repository("goodsInfoManageDAO")
public class GoodsInfoManageDAOImpl extends SuperDAO<GoodsManageInfo> implements
		GoodsInfoManageDAO {

	/**
	 * 分页查询
	 * 
	 * @param GoodsManageInfo
	 * @return
	 */
	@Override
	public Pagination<GoodsManageInfo> queryPaginationList(
			GoodsManageInfo goodsManageInfo) {
		Pagination<GoodsManageInfo> paginationList = null;
		Integer rowCount = queryRowCount(goodsManageInfo);

		goodsManageInfo.calculate();
		List<GoodsManageInfo> list = queryGoodsInfoList(goodsManageInfo);
		paginationList = this.queryPagination(rowCount, list, goodsManageInfo);

		return paginationList;
	}

	/**
	 * 查询记录总数
	 * 
	 * @param pageInruire
	 * @return
	 */
	private Integer queryRowCount(GoodsManageInfo goodsManageInfo) {
		Integer rowCount = 0;
		rowCount = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsInfoManageMapper.queryRowCount",
				goodsManageInfo);
		return rowCount;
	}

	/**
	 * 记录结果集list
	 * 
	 * @param goodsManageInfo
	 * @return
	 */
	private List<GoodsManageInfo> queryGoodsInfoList(
			GoodsManageInfo goodsManageInfo) {
		List<GoodsManageInfo> list = null;
		list = this.getSqlSession().selectList(
				"mybatis.xml.GoodsInfoManageMapper.queryGoodsInfoList",
				goodsManageInfo);

		// 下游用户编号
		String userNo = goodsManageInfo.getUserNo();
		String templateCode = null;
		// 查询下游用户表
		templateCode = this.getSqlSession().selectOne(
				"mybatis.xml.DownstreamUserInfoMapper.queryTemplateCode",
				userNo);

		List<GoodsManageInfo> goodsList = new ArrayList<GoodsManageInfo>();
		if (templateCode != null) { // 使用了模版
			for (GoodsManageInfo goodsInfo : list) {
				String goodsId = goodsInfo.getGoodsId();
				// 根据模版编号和商品编号，查询商品售价
				SpecialPriceSystemInfo saleInfo = new SpecialPriceSystemInfo();
				saleInfo.setTemplateCode(templateCode);
				saleInfo.setGoodsId(goodsId);

				String salePrice = this
						.getSqlSession()
						.selectOne(
								"mybatis.xml.SpecialPriceSystemMapper.queryGoodsSalePrice",
								saleInfo);

				if (salePrice != null && !("".equals(salePrice))) {
					int salePriceVal = Integer.parseInt(salePrice);
					goodsInfo.setCommonSalePrice(salePriceVal);
				}

				goodsList.add(goodsInfo);
			}

			return goodsList;

		} else {
			return list;
		}
	}

	/**
	 * 根据产品Id查询单条记录
	 * 
	 * @return
	 */
	@Override
	public GoodsManageInfo queryGoodsInfoByGoodsId(String goodsId) {
		GoodsManageInfo goodsManageInfo = null;
		goodsManageInfo = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsInfoManageMapper.queryGoodsInfoByGoodsId",
				goodsId);

		return goodsManageInfo;
	}

	/**
	 * 产品新增
	 * 
	 * @param GoodsManageInfo
	 */
	@Override
	public void insert(GoodsManageInfo goodsManageInfo) {
		this.getSqlSession().insert("mybatis.xml.GoodsInfoManageMapper.insert",
				goodsManageInfo);
	}

	/**
	 * 产品修改
	 * 
	 * @param goodsManageInfo
	 */
	@Override
	public void update(GoodsManageInfo goodsManageInfo) {
		this.getSqlSession().update("mybatis.xml.GoodsInfoManageMapper.update",
				goodsManageInfo);
	}

	/**
	 * 商品上下架操作
	 */
	@Override
	public void updateState(GoodsManageInfo goodsManageInfo) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsInfoManageMapper.updateState",
				goodsManageInfo);
	}

	/**
	 * 查询最大产品Id值
	 * 
	 * @return
	 */
	@Override
	public String queryMaxGoodsId() {
		String maxGoodsId = null;
		maxGoodsId = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsInfoManageMapper.querymaxGoodsId");
		return maxGoodsId;
	}

	/**
	 * 根据品牌Id，查询该品牌下的所有产品
	 * 
	 * @param brandId
	 * @return
	 */
	@Override
	public List<GoodsManageInfo> queryListByBrandId(GoodsManageInfo info) {
		List<GoodsManageInfo> list = null;
		list = this.getSqlSession().selectList(
				"mybatis.xml.GoodsInfoManageMapper.queryListByBrandId", info);

		return list;
	}

	/**
	 * 根据商品名称，查询单条记录
	 * 
	 * @param goodsName
	 * @return
	 */
	@Override
	public GoodsManageInfo queryByGoodsName(String goodsName) {
		GoodsManageInfo info = null;
		info = this.getSqlSession()
				.selectOne(
						"mybatis.xml.GoodsInfoManageMapper.queryByGoodsName",
						goodsName);

		return info;
	}

	/**
	 * 查询未加入到某一价格模版下的 所有 商品编号
	 * 
	 * @param goodsManageInfo
	 * @return
	 * @date 2012-10-23
	 */
	@Override
	public List<GoodsManageInfo> queryNotInSpecialPriceOFTemPlate(
			GoodsManageInfo goodsManageInfo) {
		List<GoodsManageInfo> list = null;
		list = this
				.getSqlSession()
				.selectList(
						"mybatis.xml.GoodsInfoManageMapper.queryNotInSpecialPriceOFTemPlate",
						goodsManageInfo);

		return list;
	}

	@Override
	public List<GoodsManageInfo> queryGoodsIdAndNameNotInRule() {
		List<GoodsManageInfo> list = this
				.getSqlSession()
				.selectList(
						"mybatis.xml.GoodsInfoManageMapper.queryGoodsIdAndNameNotInRule");
		return list;
	}

	/**
	 * 根据商品编号，查询符合条件的记录
	 * 
	 * @param goodsManageInfo
	 * @return
	 */
	@Override
	public GoodsManageInfo queryRecordByGoodsId(GoodsManageInfo goodsManageInfo) {
		GoodsManageInfo info = null;
		info = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsInfoManageMapper.queryRecordByGoodsId",
				goodsManageInfo);
		return info;
	}

	@Override
	public List<GoodsManageInfo> queryAllGoodsId() {

		List<GoodsManageInfo> info = this.getSqlSession().selectList(
				"mybatis.xml.GoodsInfoManageMapper.queryAllGoodsId");
		return info;
	}

	@Override
	public String queryGoodsNameByGoodsId(String goodsId) {
		String goodsName = null;
		goodsName = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsInfoManageMapper.queryGoodsNameByGoodsId",
				goodsId);
		return goodsName;
	}

	@Override
	public void deleteByGoodsId(String goodsId) {
		this.getSqlSession().delete(
				"mybatis.xml.GoodsInfoManageMapper.deleteByGoodsId", goodsId);
	}

	@Override
	public void updateByBrandId(GoodsManageInfo goodsManageInfo) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsInfoManageMapper.updateByBrandId",
				goodsManageInfo);

	}

}
